<?php
##########################################
##### Autor: Yngrid Coello           #####
##### Correo: y-c-@hotmail.com       #####
##### Ult. Modif: 26/10/2011         #####
##### Pasantias de Grado - USB       #####
##### Realización del Sistema: 0Bs   #####
##########################################

class aeronaveActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
	// Se define la cantidad de resultados que se quieren por pagina
	$pager = new sfPropelPager('Aeronave', sfConfig::get('app_max_aeronaves'));
	$pager->setCriteria(new Criteria());
	// Se establece que la pagina principal del index sera la 1
	$pager->setPage($this->getRequestParameter('page', 1));
	$pager->init();
	 // Se Prepara la informaci�n para usarla en la vista.
	$this->pager = $pager;
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->Aeronave = AeronavePeer::retrieveByPk($request->getParameter('serial'));
    $this->forward404Unless($this->Aeronave);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new AeronaveForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new AeronaveForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($Aeronave = AeronavePeer::retrieveByPk($request->getParameter('serial')), sprintf('Object Aeronave does not exist (%s).', $request->getParameter('serial')));
    $this->form = new AeronaveForm($Aeronave);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($Aeronave = AeronavePeer::retrieveByPk($request->getParameter('serial')), sprintf('Object Aeronave does not exist (%s).', $request->getParameter('serial')));
    $this->form = new AeronaveForm($Aeronave);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($Aeronave = AeronavePeer::retrieveByPk($request->getParameter('serial')), sprintf('Object Aeronave does not exist (%s).', $request->getParameter('serial')));
    $Aeronave->delete();
    $numero = $request->getParameter('serial');
    LogPeer::logBorrar($numero,$this->getUser(),'Aeronave');
    $this->redirect('aeronave/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
        try {
            $Aeronave = $form->save();
            $serial = $request->getPostParameter('aeronave[serial]');
            LogPeer::log($serial,$this->getUser(),'Aeronave');
            $this->redirect('aeronave/index');
        }catch (Exception $e){
            $this->getUser()->setFlash('error', 'No se puede registrar esta aeronave, el serial ya existe.');
            return sfView::NONE;
        }
    }
  }
}
